﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AyuBookmark.Common.Interfaces;
using AyuBookmark.Common.Models;
using Microsoft.Extensions.Logging;

namespace AyuBookmark.Service.Services
{
    /// <summary>
    /// 通知服务实现
    /// </summary>
    public class NotificationService : INotificationService
    {
        private readonly INotificationRepository _notificationRepository;
        private readonly ILogger<NotificationService> _logger;
        private readonly IUserRepository _userRepository;
        private readonly INotificationService _notificationService;


        //public NotificationService(
        //    INotificationRepository notificationRepository,
        //    ILogger<NotificationService> logger)
        //{
        //    _notificationRepository = notificationRepository;
        //    _logger = logger;
        //}
        public NotificationService(
            INotificationRepository notificationRepository,
            IUserRepository userRepository,
            ILogger<NotificationService> logger,
            INotificationService notificationService)
        {
            _notificationRepository = notificationRepository;
            _userRepository = userRepository;
            _logger = logger;
            _notificationService = notificationService;
        }

        public async Task<PaginatedResult<Notification>> GetPagedNotificationsAsync(
    int userId,
    int pageIndex = 1,
    int pageSize = 20)
        {
            return await _notificationRepository.GetPagedAsync(
                n => n.UserId == userId,
                pageIndex,
                pageSize,
                n => n.CreatedTime,
                true);
        }

        /// <inheritdoc />
        public async Task SendAsync(Notification notification)
        {
            //await _notificationRepository.AddAsync(notification);
            //_logger.LogInformation("Sent notification to user {UserId}, title: {Title}",
            //    notification.UserId, notification.Title);
            var user = await _userRepository.GetByIdAsync(notification.UserId);
            if (user == null)
            {
                _logger.LogWarning("Attempted to send notification to non-existent user: {UserId}",
                    notification.UserId);
                throw new ArgumentException("User not found");
            }

            await _notificationRepository.AddAsync(notification);
            _logger.LogInformation("Sent notification to user {UserId}, title: {Title}",
                notification.UserId, notification.Title);
        }

        /// <inheritdoc />
        public async Task<List<Notification>> GetUnreadNotificationsAsync(int userId)
        {
            return (await _notificationRepository.FindAsync(n =>
                n.UserId == userId && !n.IsRead))
                .OrderByDescending(n => n.CreatedTime)
                .ToList();
        }

        /// <inheritdoc />
        public async Task MarkAsReadAsync(int notificationId)
        {
            var notification = await _notificationRepository.GetByIdAsync(notificationId);
            if (notification != null)
            {
                notification.IsRead = true;
                notification.ReadTime = DateTime.Now;
                await _notificationRepository.UpdateAsync(notification);
            }
        }

        /// <inheritdoc />
        public async Task<PaginatedResult<Notification>> GetAllNotificationsAsync(int userId, int pageIndex = 1, int pageSize = 20)
        {
            //return await _notificationRepository.GetPagedAsync(
            //    n => n.UserId == userId,
            //    pageIndex,
            //    pageSize,
            //    n => n.CreatedTime,
            //    true);
            return  _notificationService.GetPagedNotificationsAsync(userId, pageIndex, pageSize).Result;
        }
    }
}